package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.entity.Emp;
import com.itheima.pojo.dto.EmpQuery;
import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.vo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping("/list")
    public Result findAll(){
        log.info("查询全部员工");
        List<Emp> empList = empService.findAll();
        return Result.success(empList);
    }

    //分页查询
    @GetMapping
    public Result page(EmpQuery empQuery){
        log.info("分页查询,参数:{}",empQuery);
        PageResult empList = empService.page(empQuery);
        return Result.success(empList);
    }

    //根据id查询员工
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }

    //新增员工
    @PostMapping()
    @Log
    public Result save(@RequestBody Emp emp){
        log.info("新增员工,员工信息:{}",emp);
        empService.saveEmp(emp);
        return Result.success();
    }

    //修改员工
    @PutMapping
    @Log
    public Result update(@RequestBody Emp emp){
        log.info("修改员工,员工信息:{}",emp);
        empService.updateEmp(emp);
        return Result.success();
    }
    //删除员工
    @DeleteMapping
    @Log
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除员工，ids:{}",ids);
        empService.delete(ids);
        return Result.success();
    }



}
